﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockImport.dataimport.common;
using Utilities;
using StockImport.http;
using StockCommon.Calendar;
using StockCommon.TaskLog;
using NetLib.web;

namespace StockImport.dataimport.tse.dailyindex
{
    public class TSE_DailyIndexFetcher : HttpContentFetcher_Daily
    {

        protected Guid taskId;
        public TSE_DailyIndexFetcher()
        {
            taskId = ObjectExecServices.getServicesId("sitefetcher", getTaskName(), getVersion());
        }


        public override string getURL(DateTime d)
        {
            String url = String.Format("http://www.twse.com.tw/ch/trading/exchange/MI_INDEX/genpage/Report{0}/A112{1}ALLBUT0999_1.php?select2=ALLBUT0999&chk_date={2}", DateUtils.getYYYYMMPlanString(d), DateUtils.getPlanString(d), DateUtils.getROCString(d));
            return url;
        }

        public override DateTime getStartDate()
        {
            return DateTime.Parse("2004/02/11");
        }
        public override DateTime getEndDate()
        {
            return DateTime.Parse("2014/01/01"); 
        }

        public override string getTaskName()
        {
            return "上市_收盤行情";
        }

        public override string getVersion()
        {
            return "2.0";
        }

        public String getStoreFileName(DateTime dt)
        {
            String ds = DateUtils.toShortDateFormat_NoSlash(dt);
            return @"D:\stockdata\tse\上市_收盤行情\"+ds+".html";
        }
       public override ProcessResultEnum Update(DateTime date, bool force = false)
        {
            date = DateUtils.getDateOnly(date);
            UpdateResult updateResult = new UpdateResult();
            updateResult.操作 = "上市_收盤行情抓取";
            updateResult.資料日期 = date;


            if (CalenderHelper.isWorkDay(date) == false)
                return ProcessResultEnum.完成且不需更新;

            DateTime dt = DateUtils.getTimeAtDay(date, 14);
            if (DateTime.Now.CompareTo(dt) < 0)
                return ProcessResultEnum.必須重做;

            //取的ObjectExecLog
            StockCommon.ObjectExecLog log = ObjectExecServices.getExecLog(taskId, "get", date);
            if (force == false)
                if (islogComplete(log))
                    return ProcessResultEnum.完成且不需更新;
            log.lastExecuteStart = DateTime.Now;
            //----------------------------------------------------
            //Remove. Data.
            //----------------------------------------------------
            String fileName = getStoreFileName(date);

            String url = getURL(date);

            bool success = false;
            String opMessage = "";
            String content = HttpGet.process(url, out success, out opMessage);
            log.lastExecuteEnd = DateTime.Now;


            ProcessResultEnum result;
            if (success)
            {
                log.status = (int)LogStatus.sUpdated;
                if (String.IsNullOrWhiteSpace(content) == false)
                {
                    FileUtils.saveUTF8(fileName, content);
                    log.result = (int)LogResult.rComplete;
                    result = ProcessResultEnum.完成且有資料;
                    log.msg = "資料長度:" + (content.Length / 1000).ToString() + "KB";
                }
                else
                {
                    log.result = (int)LogResult.rNoData;
                    log.msg = "無資料";
                    result = ProcessResultEnum.必須重做;
                }

            }
            else
            {
                log.status = (int)LogStatus.sConnectFailed;
                log.msg = opMessage;
                result = ProcessResultEnum.發生錯誤;
            }
            ObjectExecServices.updateExecLog(log);
            return result;

        }

    }
}
